<template>
  <div>
    <a-modal v-model="visible" title="仓库配置" width="50%">
      <template slot="footer">
        <a-button
          @click="visible = false"
          style="background: #d7d7d7; color: #fff; border: none"
          >关闭</a-button
        >
      </template>
      <a-table
        bordered
        size="small"
        :columns="columns"
        :data-source="warehouseTable"
        :pagination="false"
      >
        <!-- //状态 -->
        <span slot="key" slot-scope="text, record">
          <a-switch :disabled="!record.isEdit" v-model="record.switch" />
        </span>
        <!-- 收费方案 -->
        <span slot="tenantId" slot-scope="text, record">
          <a-select
            v-model="record.sysChargingSchemeId"
            v-if="record.isEdit"
            style="width: 100%"
            show-search
            option-filter-prop="children"
            placeholder="收费方案"
            @change="(value, option) => onSchemeChange(value, option, record)"
          >
            <a-select-option
              :titles="item.schemeName"
              :value="item.sysChargingSchemeId"
              v-for="item in revenuePlans"
              :key="item.sysChargingSchemeId"
            >
              {{ item.schemeName }}
            </a-select-option>
          </a-select>
          <span v-if="!record.isEdit">
            {{ record.schemeName === null ? "暂无" : record.schemeName }}
          </span>
        </span>
        <!-- //有效时间 -->
        <span slot="size" slot-scope="text, record">
          <a-range-picker
            :allowClear="false"
            v-if="record.isEdit"
            :value="
              record.startDate === undefined ||
              record.endtDate === undefined ||
              record.startDate === null ||
              record.endtDate === null
                ? null
                : [
                    moment(record.startDate, 'YYYY-MM-DD'),
                    moment(record.endtDate, 'YYYY-MM-DD'),
                  ]
            "
            @change="
              (date, dateString) => takeEffectTime(date, dateString, record)
            "
          />
          <span v-if="!record.isEdit">
            {{
              record.startDate === null
                ? "暂无"
                : record.startDate.substring(0, 10)
            }}~{{
              record.endtDate === null
                ? "暂无"
                : record.endtDate.substring(0, 10)
            }}
          </span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a
            v-if="!record.isEdit"
            href="javascript:;"
            style="text-decoration: underline; color: rgb(41, 41, 224)"
            @click="record.isEdit = true"
            >编辑</a
          >
          <a
            v-if="record.isEdit"
            href="javascript:;"
            style="text-decoration: underline; color: rgb(41, 41, 224)"
            @click="savaRowDate(record)"
            >保存</a
          >
          <a-divider v-if="record.isEdit" type="vertical" />
          <a
            v-if="record.isEdit"
            href="javascript:;"
            style="text-decoration: underline; color: #f00"
            @click="cancel(record)"
            >取消</a
          >
        </span>
      </a-table>
    </a-modal>
  </div>
</template>

<script>
import moment from "moment";
import { getRevenPlanList } from "@/apis/system/setManage/setWarehouse";
import { acc_addCrsHouse } from "@/apis/system/acc/acc";
import { getWhList } from "@/apis/common";
export default {
  data() {
    return {
      moment,
      visible: false,
      //全部仓库
      warehouseTable: [],
      //账号所拥有仓库
      accWarehouseList: [],
      //每一行客户ID
      rowSerialNumber: "",
      //收费方案
      revenuePlans: [],
    };
  },
  mounted() {
    this.$bus.$off("openWarehouse");
    this.$bus.$on("openWarehouse", (rowData) => {
      this.rowData = { ...rowData };
      this.rowSerialNumber = rowData.serialNumber;
      this.getModalData(rowData.serialNumber);
    });
  },
  methods: {
    cancel(record) {
      record.isEdit = false;
      this.getModalData(this.rowSerialNumber);
    },
    getModalData(id) {
      this.getWareHouseList(id);
      this.getRevenuePlans();
      this.visible = true;
    },
    //收费方案选择
    onSchemeChange(value, option, record) {
      record.sysChargingSchemeId = value;
      record.schemeName = option.data.attrs.titles;
    },
    //获取收费方案
    async getRevenuePlans() {
      let res = await getRevenPlanList({ size: 10000, current: 1 });
      if (res.code == "200") {
        this.revenuePlans = res.responseBody.records;
      }
    },
    //生效时间
    takeEffectTime(date, dateString, record) {
      record.startDate = dateString[0];
      record.endtDate = dateString[1];
    },
    //保存每一行数据
    async savaRowDate(record) {
      if (record.switch) {
        if (
          (record.startDate ?? "" !== "") &&
          (record.sysChargingSchemeId ?? "" !== "")
        ) {
          let { code } = await acc_addCrsHouse({
            csrId: this.rowSerialNumber,
            state: record.switch ? "1" : "2",
            warehouseId: record.sysStoreHouseId,
            startDate: record.startDate,
            endtDate: record.endtDate,
            sysChargingSchemeId: record.sysChargingSchemeId,
            schemeName: record.schemeName,
            id: record.scwId === null ? null : record.scwId,
          });
          if (code === "10000") {
            record.isEdit = false;
            this.getModalData(this.rowSerialNumber);
          }
        } else {
          this.$message.error("仓库套餐或生效时间未选择！");
          return false;
        }
      } else {
        let { code } = await acc_addCrsHouse({
          csrId: this.rowSerialNumber,
          state: record.switch ? "1" : "2",
          warehouseId: record.sysStoreHouseId,
          startDate: record.startDate,
          endtDate: record.endtDate,
          sysChargingSchemeId: record.sysChargingSchemeId,
          schemeName: record.schemeName,
          id: record.scwId === null ? null : record.scwId,
        });
        if (code === "10000") {
          record.isEdit = false;
          this.getModalData(this.rowSerialNumber);
        }
      }
    },
    //获取仓库
    async getWareHouseList(id) {
      let { code, responseBody } = await getWhList(id);
      if (code === "200") {
        responseBody.forEach((item) => {
          item.isEdit = false;
          if (item.warehouseId) {
            item.switch = true;
          } else {
            item.switch = false;
          }
        });
        this.warehouseTable = responseBody;
      }
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
  },
  computed: {
    columns() {
      return [
        {
          title: "仓库名",
          dataIndex: "storeHouseCode",
          align: "center",
          width: "15%",
        },
        {
          title: "状态",
          dataIndex: "key",
          align: "center",
          scopedSlots: { customRender: "key" },
          width: "15%",
        },
        {
          title: "方案套餐",
          dataIndex: "tenantId",
          align: "center",
          scopedSlots: { customRender: "tenantId" },
          width: "25%",
        },
        {
          title: "有效时间",
          dataIndex: "size",
          align: "center",
          scopedSlots: { customRender: "size" },
          width: "30%",
        },
        {
          title: "操作",
          dataIndex: "action",
          align: "center",
          scopedSlots: { customRender: "action" },
          width: "15%",
        },
      ];
    },
  },
};
</script>

<style lang="less" scoped>
</style>